package generic;


import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/11/13
 * Time: 9:06
 * Description:
 */
class MyStack<T> {
    public T[] elem = (T[])new Object[2];
    public int usedSize;

    public void push(T val) {
        this.elem[usedSize++] = val;
    }

    public T getTop() {
        return this.elem[usedSize-1];
    }
}
//写一个泛型类，找到泛型数组当中的最大值
class Alg<T extends Comparable<T>> {

    public T findMax(T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}
/*class A {

}*/
class Alg2 {

    public static<T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class GenericTest {

    public static<T> void print(ArrayList<T> list) {
        for (T t:list) {
            System.out.println(t);
        }
    }

    public static void print2(ArrayList<?> list) {
        for (Object t:list) {
            System.out.println(t);
        }
    }


}

public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        GenericTest.print2(arrayList);

    }

    public static void main5(String[] args) {
        Integer[] array = {1,12,31,4};
        System.out.println(Alg2.findMax(array));
        System.out.println(Alg2.<Integer>findMax(array));
    }

    public static void main4(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] array = {1,12,31,4};
        System.out.println(alg.findMax(array));
    }

    public static void main3(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Alg<String> alg2 = new Alg<>();
        //Alg<A> alg3 = new Alg<>();

    }

    public static void main2(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push("fafafda");
    }

    public static void main1(String[] args) {
        MyStack<Integer> myStack = new MyStack<>();
        myStack.push(1);
        int val = myStack.getTop();
        MyStack<String> myStack2 = new MyStack<>();
    }
}
